package com.ruifu.conversions.utils;

import com.aspose.cad.Color;
import com.aspose.cad.Image;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PdfOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author lcq
 * @create 2020/4/17
 */
@Slf4j
@Component
public class Dwg4Pdf {

    public static File Dwg2Pdf(File dwgFile) throws IOException {

        FileInputStream fileInputStream;
        //将pdf文件转换成InputStream输入流
        fileInputStream = new FileInputStream(dwgFile);

        Image objImage = Image.load(fileInputStream);

        CadRasterizationOptions rasterizationOptions = new  CadRasterizationOptions();

        //设置颜色
        rasterizationOptions.setBackgroundColor(Color.getBlack());
        rasterizationOptions.setPageWidth(1400);
        rasterizationOptions.setPageHeight(650);
        rasterizationOptions.setAutomaticLayoutsScaling(true);
        rasterizationOptions.setNoScaling (false);
        rasterizationOptions.setDrawType(1);

        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setVectorRasterizationOptions(rasterizationOptions);

        //输出文件
        File outputFile = new File(dwgFile.getName().substring(0,dwgFile.getName().lastIndexOf("."))+".pdf");
        //存放地址
        objImage.save(outputFile.getPath(), pdfOptions);
        log.info(dwgFile.getName()+"文件转换成功");
        fileInputStream.close();
        return outputFile;
    }
}
